<template>
  <div class="book-selector-plus">
    <div class="book-selector-plus__input-wrap">
      <Input
        placeholder="请输入书籍或者作者名称"
        class="book-selector-plus__input"
        v-model="search"
        @on-enter="handleSearch"
      />
      <Button type="primary" icon="ios-search" @click="handleSearch">
        开始搜索
      </Button>
    </div>
    <Card class="book-selector-plus__content">
      <div class="book-selector-plus__left">
        <p class="book-selector-plus__title">
          搜索结果（共计{{ searchBooks.length }}条）
        </p>
        <div class="book-selector-plus__left-inner">
          <p v-if="searchBooks.length === 0" class="book-selector-plus__none">
            未找到相关书籍，试试换换关键字
          </p>
          <template v-else>
            <div
              v-for="item in searchBooks"
              :key="item._id"
              class="book-selector-plus__book"
              @click="handleSelect(item)"
            >
              <div
                :class="{
                  'book-selector-plus__book-img': true,
                  selected: item.selected,
                }"
              >
                <img :src="item.img_url" alt="书籍图片" />
                <Icon v-if="item.selected" type="ios-checkmark" />
              </div>
              <div class="book-selector-plus__book-detail">
                <p>{{ item.name }}</p>
                <p>{{ item.author }}</p>
                <p class="two-row-ellipsis">{{ item.des }}</p>
              </div>
            </div>
          </template>
        </div>
      </div>
      <div class="book-selector-plus__right">
        <p class="book-selector-plus__title">
          已选中书籍（共计{{ selects.length }}条）
        </p>
        <div class="book-selector-plus__right-inner">
          <p v-if="selects.length === 0" class="book-selector-plus__none">
            还未选择书籍
          </p>
          <template v-else>
            <div
              v-for="item in selects"
              :key="item._id"
              class="book-selector-plus__book2"
            >
              <img :src="item.img_url" alt="书籍图片" />
              <div class="book-selector-plus__book2-detail">
                <p>{{ item.name }}</p>
                <p>{{ item.author }}</p>
                <p class="two-row-ellipsis">{{ item.des }}</p>
              </div>
            </div>
          </template>
        </div>
      </div>
    </Card>
  </div>
</template>

<script>
import http from "@/utils/http";
import debounce from "@/utils/debounce";

const doSearch = debounce(
  (value, self) => {
    http({
      method: "GET",
      url: "/api/admin/book/search",
      params: { keyword: value },
      operation: "搜索书籍",
    }).then((res) => {
      if (res && res.ok) {
        self.books = res.data.list;
      }
    });
  },
  300,
  true
);

export default {
  name: "m-book-selector-plus",

  data() {
    return {
      search: "",
      books: [],
      selects: [],
    };
  },

  computed: {
    searchBooks() {
      return this.books.map((item) => {
        item.selected = this.selects.some((book) => book._id === item._id);
        return item;
      });
    },
  },

  methods: {
    handleSearch() {
      doSearch(this.search, this);
    },

    handleSelect(item) {
      const selected = this.selects.some((book) => book._id === item._id);
      if (selected) {
        this.selects = this.selects.filter((book) => book._id !== item._id);
      } else {
        delete item.selected;
        this.selects.push(item);
      }
      this.$emit(
        "update:value",
        this.selects.map((item) => item._id)
      );
    },
  },

  created() {
    doSearch("", this);
  },
};
</script>

<style lang="scss">
.book-selector-plus {
  &__input {
    display: block;
    width: 60%;
    border: none;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
    margin-right: 10px;

    .ivu-input {
      border: none;
    }

    &-wrap {
      display: flex;
      flex-flow: row nowrap;
      justify-content: center;
      align-items: center;
    }
  }

  &__content {
    margin-top: 20px;
    border: none;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);

    .ivu-card-body {
      display: flex;
      flex-flow: row nowrap;
      width: 100%;
      height: 400px;
      padding: 0;
    }
  }

  &__left {
    flex: 1;
    height: 100%;
    display: flex;
    flex-flow: column nowrap;
    justify-content: flex-start;
    align-items: flex-start;
    padding: 12px;

    &-inner {
      flex: 1;
      display: flex;
      flex-flow: row wrap;
      justify-content: flex-start;
      align-items: flex-start;
      overflow: auto;
      width: 100%;

      &::-webkit-scrollbar {
        width: 0px;
      }
    }
  }

  &__title {
    margin-bottom: 12px;
  }

  &__book {
    width: 45%;
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: flex-start;
    margin-right: 10%;
    margin-bottom: 10px;
    cursor: pointer;

    &:nth-child(even) {
      margin-right: 0;
    }

    &-img {
      position: relative;
      display: block;
      margin-right: 10px;

      & > img {
        width: 60px;
        height: 90px;
        border-radius: 4px;
      }

      &.selected .ivu-icon {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #fff;
        font-size: 40px;
        z-index: 1;
      }

      &.selected::after {
        content: " ";
        display: block;
        width: 100%;
        height: 100%;
        position: absolute;
        background: rgba(0, 0, 0, 0.6);
        top: 0;
        left: 0;
      }
    }

    &-detail {
      & > p:first-child {
        font-weight: bold;
        color: #666;
      }

      & > p:nth-child(2) {
        font-weight: bold;
        color: #999;
        font-size: 12px;
        transform: scale(0.8);
        position: relative;
        left: -14px;
      }

      & > p:last-child {
        font-weight: bold;
        color: #999;
        font-size: 12px;
        transform: scale(0.8);
        position: relative;
        left: -14px;
      }
    }
  }

  &__book2 {
    width: 100%;
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: flex-start;
    margin-bottom: 20px;
    cursor: pointer;

    &:nth-child(even) {
      margin-right: 0;
    }

    & > img {
      display: block;
      margin-right: 10px;
      width: 60px;
      height: 90px;
      border-radius: 4px;
    }

    &-detail {
      & > p:first-child {
        font-weight: bold;
        color: #666;
      }

      & > p:nth-child(2) {
        font-weight: bold;
        color: #999;
        font-size: 12px;
        transform: scale(0.8);
        position: relative;
        left: -16px;
      }

      & > p:last-child {
        font-weight: bold;
        color: #999;
        font-size: 12px;
        transform: scale(0.8);
        position: relative;
        left: -16px;
      }
    }
  }

  &__right {
    width: 240px;
    height: 100%;
    border-left: 1px solid #dcdee2;
    padding: 12px;
    display: flex;
    flex-flow: column nowrap;
    justify-content: flex-start;
    align-items: flex-start;

    &-inner {
      flex: 1;
      overflow: auto;
      width: 100%;

      &::-webkit-scrollbar {
        width: 0px;
      }
    }
  }

  &__none {
    text-align: center;
    font-size: 12px;
    color: #999;
    display: block;
    width: 100%;
    height: 60px;
    line-height: 60px;
  }
}
</style>
